<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>推荐位管理</el-breadcrumb-item>
        <el-breadcrumb-item>阅读器</el-breadcrumb-item>
        <el-breadcrumb-item>独立页推荐位</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_admin_name }}</el-tag>
        </template>
        <template slot-scope="scope" slot="menuLeft">
          <el-tag type="danger">
            <b>*指定书推荐|全局推荐最多支持配置20章</b>
          </el-tag>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import {
  appConfigReadIndependent as baseURL,
  // appPopup,
  selectVersion,
} from "@/api/cat/baseUrls";
export default {
  data() {
    var validatePass = (rule, value, callback)=>{
      if (value === '') {
        callback(new Error('请输入书籍id'));
      }
      // else if(!(/^[-,0-9]+$/.test(value))){
      //   callback(new Error('书籍id为数字，多个书籍id用英文逗号隔开'));
      // }
       else {
        callback();
      }
    };
    return {
      radio: "1",
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {},
      oldForm: {},
      default: {
        orderByList: "book_id&ASC,preference&ASC,chapter_display&ASC",
      },
      option: {
        border: true,
        tip: true,
        viewBtn: false,
        editBtn: true,
        addBtn:true,
        delBtn:true,
        align: "center",
        headerAlign: "center",
        column: [
          {
            label: "推荐位标题",
            prop: "title",
            type: "input",
             rules: [
              {
                required: true,
                message: "推荐位标题必须填写",
              },
            ],
          },

          {
            label: "推荐语",
            prop: "intro",
            type: "input",
            rules: [
              {
                required: true,
                message: "推荐语必须填写",
              },
            ],
          },
          {
            label: "语言",
            prop: "language",
            type: "select",
            search: true,
            placeholder: "请选择语言",
            dicData: [
              {
                label: "英语",
                value: 2,
              },
              {
                label: "芬兰语",
                value: 3,
              }
            ],
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "请选择语言类型",
              },
            ]
          },
          {
            label: "配置方式",
            prop: "type",
            type: "select",
            search: true,
            dicData: [
              { label: "指定书籍推荐", value: 1 },
              { label: "全局推荐", value: 2 },
            ],
            rules: [
              {
                required: true,
                message: "配置方式必须填写",
              },
            ],
          },
          {
            label: "指定书籍id",
            prop: "book_id",
            type: "input",
          },
          {
            label: "推荐位样式",
            prop: "style",
            type: "select",
            search: true,
            dicData: [
              { label: "多图", value: 2 },
              { label: "单图", value: 1 },
            ],
            rules: [
              {
                required: true,
                message: "推荐位样式必须填写",
              },
            ],
          },
          {
            label: "阅读n章后出现",
            prop: "chapter_display",
            type: "number",
            rules: [
              {
                required: true,
                message: "阅读n章必须填写",
              },
            ],
          },
          {
            label: "偏好",
            prop: "preference",
            type: "select",
            search: true,
            dicData: [
              { label: "女", value: 2 },
              { label: "男", value: 1 },
            ],
            rules: [
              {
                required: true,
                message: "偏好必须填写",
              },
            ],
          },
          {
            label: "书籍id",
            prop: "book_ids",
            type: "input",
            rules: [{ required:true,validator: validatePass, trigger: 'blur' }]
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            slot: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      response.data.forEach(item => {
        item.book_ids = item.book_ids.toString()
      });
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      if(row.type==2){
        row.book_id = null;
      }
      delete row["id"];
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.book_ids = (row.book_ids+ '').split(",")
      let data = await common(baseURL.save, row, null, "POST");
      loading()
      if (data == null) {
        return;
      }
      console.log(row)
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      if(row.type==2){
        row.book_id = '';
      }
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.book_ids = (row.book_ids+ '').split(",")
      let data = await common(baseURL.update, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {  
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.delete,
            { id: row.id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {
      //   params.popup_name = `%${params.popup_name ? params.popup_name : ""}%`;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  }
};
</script>

<style>
</style>